package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner my_spinner1;
    private Spinner my_spinner2;
    private Spinner my_spinner3;
    private List<CharSequence> palceList1 = null;
    private List<CharSequence> palceList2 = null;
    private List<CharSequence> palceList3 = null;

    private ArrayAdapter<CharSequence> eduAdapter1 = null;
    private ArrayAdapter<CharSequence> eduAdapter2 = null;
    private ArrayAdapter<CharSequence> eduAdapter3 = null;


    private static final String TAG = "MainActivity";
    String jsonstr = "[\n" +
            "    { \"id\":\"1\",\"name\":\"张三\",\"age\":\"18\" },\n" +
            "    { \"id\":\"2\",\"name\":\"李四\",\"age\":\"18\"  },\n" +
            "    { \"id\":\"3\",\"name\":\"王五\",\"age\":\"18\" }\n" +
            "]";

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        解析json字符串
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    JSONArray jsonArray = new JSONArray(jsonstr);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String id = jsonObject.getString("id");
                        String name = jsonObject.getString("name");
                        String age = jsonObject.getString("age");
                        Log.e(TAG, " id = " + id + " name = " + name + " age = " + age);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        final Student student1 = new Student("安安", 88, "1236");
        final Student student2 = new Student("弟弟", 22, "1237");
        final Student student3 = new Student("大大", 33, "1238");
        final Student student4 = new Student("等等", 22, "1239");
        final ArrayList<Student> studentArrayList = new ArrayList<>();
        studentArrayList.add(student1);
        studentArrayList.add(student2);
        studentArrayList.add(student3);
        studentArrayList.add(student4);


        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("name", student1.getName());
                    jsonObject.put("age", student1.getAge());
                    jsonObject.put("stuNo", student1.getStuNo());
                    Log.e(TAG, "jsonObject = " + jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try {
                    JSONArray jsonArray = new JSONArray();

                    for (Student student : studentArrayList) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name", student.getName());
                        jsonObject.put("age", student.getAge());
                        jsonObject.put("sutNo", student.getStuNo());

                        jsonArray.put(jsonObject);

                    }
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("stuList", jsonArray);

                    Log.e(TAG, "jsonObject = " + jsonObject.toString());


                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    InputStream inputStream = getResources().getAssets().open("data.txt");
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuffer stringBuffer = new StringBuffer();
                    String line = bufferedReader.readLine();
                    while (line != null) {
                        stringBuffer.append(line);
                        line = bufferedReader.readLine();
                    }
                    Log.e(TAG, "data = " + stringBuffer.toString());

                    JSONArray jsonArray = new JSONArray(stringBuffer.toString());


                    my_spinner1 = findViewById(R.id.Spinner01);
                    my_spinner2 = findViewById(R.id.Spinner02);
                    my_spinner3 = findViewById(R.id.Spinner03);


                    palceList1 = new ArrayList<CharSequence>();
                    palceList2 = new ArrayList<CharSequence>();
                    palceList3 = new ArrayList<CharSequence>();

                    if (!palceList1.isEmpty()) {
                        palceList1.clear();
                    }


                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String province = jsonObject.getString("name");


                        palceList1.add(province);
                        eduAdapter1 = new ArrayAdapter<CharSequence>
                                (MainActivity.this, android.R.layout.simple_spinner_item, palceList1);
                        eduAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        my_spinner1.setAdapter(eduAdapter1);


                        if (province.equals("北京市")) {
                            JSONArray cityArray = jsonObject.getJSONArray("city");

                            if (!palceList2.isEmpty()) {
                                palceList2.clear();
                            }

                            for (int j = 0; j < cityArray.length(); j++) {
                                JSONObject cityObject = cityArray.getJSONObject(j);
                                String cityName = cityObject.getString("name");


                                palceList2.add(cityName);
                                eduAdapter2 = new ArrayAdapter<CharSequence>
                                        (MainActivity.this, android.R.layout.simple_spinner_item, palceList2);
                                eduAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                my_spinner2.setAdapter(eduAdapter2);

                                Log.e(TAG, "cityName = " + cityName);


                                if (cityName.equals("北京市")) {
                                    JSONArray areaArray = cityObject.getJSONArray("area");

                                    if (!palceList3.isEmpty()) {
                                        palceList3.clear();
                                    }

                                    for (int k = 0; k < areaArray.length(); k++) {
                                        String areaName = areaArray.getString(k);
                                        Log.e(TAG, "areaName = " + areaName);

                                        palceList3.add(areaName);
                                        eduAdapter3 = new ArrayAdapter<CharSequence>
                                                (MainActivity.this, android.R.layout.simple_spinner_item, palceList3);
                                        eduAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                        my_spinner3.setAdapter(eduAdapter3);
                                    }
                                }
                            }
                        }
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
